package com.zsy.provider.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.zsy.base.bean.DemoResult;
import com.zsy.common.fastdfs.FastDfsClientUtil;
import com.zsy.dao.model.CgsDksq;
import com.zsy.dao.model.TbUser;
import com.zsy.provider.api.feign.UacFeignApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.util.Map;

@RestController
@RefreshScope
@RequestMapping("demo")
@Slf4j
@Api(tags="demo测试")
public class TestController {

    @Autowired
    UacFeignApi uacFeignApi;

    @Value("${zhao.shou.yun}")
    String zhao;

    @Autowired
    TokenStore tokenStore;

    @Autowired
    FastDfsClientUtil fastDfsClientUtil;



    @ApiOperation(value="获取所有用户数据", notes="获取所有用户数据")
    @PostMapping("/")
    public DemoResult getAllDate(){
        //TbPermission tbPermission = new TbPermission();
        return uacFeignApi.listUser("");
    }

    @ApiOperation(value="获取单个用户信息", notes="通过用户名获取用户信息")
    @GetMapping("/view/{userName}")
    public DemoResult getAllDate(@PathVariable("userName")String  userName, @ApiIgnore OAuth2Authentication user){
       // TbPermission tbPermission = new TbPermission();

      //  Map<String, Object> extraUserInfo = getExtraUserInfo(user);
       // log.info(JSON.toJSONString(extraUserInfo));
        DemoResult<TbUser> demoResult = uacFeignApi.getUserByUserName(userName);
       // demoResult.setMsg(JSON.toJSONString(extraUserInfo));
      /*  DemoResult demoResult1 = testFastDfs();
        demoResult.setMsg(JSON.toJSONString(demoResult1));*/
        return demoResult;
    }

    @ApiOperation(value="更新用户信息", notes="通过用户名更新用户信息")
    @GetMapping("/updateUserByUserName")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "userName", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "phone", value = "手机号", required = false, dataType = "String")
    })
    public DemoResult updateUserByUserName( String  userName,String phone, @ApiIgnore OAuth2Authentication user) {
        DemoResult<TbUser> tbUserDemoResult = uacFeignApi.updateUserByUserName(userName, phone);
        return tbUserDemoResult;
    }

        @ApiOperation(value="获取车购税信息", notes="通过查询条件")
    @PostMapping("/listCgsDksq")
    @DynamicParameters(properties = {
            @DynamicParameter(name = "fpdm",value = "发票代码",example = "134030822202",required = true,dataTypeClass = String.class),
            @DynamicParameter(name = "fphm",value = "发票号码",example = "12300015",required = true,dataTypeClass = String.class),
            @DynamicParameter(name = "nsrmc",value = "纳税人名称"),

    })
    public DemoResult<Page<CgsDksq>> listCgsDksq(@RequestBody JSONObject jsonObject, @ApiIgnore OAuth2Authentication user){
        DemoResult<Page<CgsDksq>> cgsDksqDemoResult = uacFeignApi.listCgsDksq(jsonObject);
        return cgsDksqDemoResult;
    }


    DemoResult testFastDfs(){
        File file = new File("F:\\1.jpg");
        DemoResult<String> stringDemoResult = fastDfsClientUtil.uploadFile(file);
        log.info("上传结果：{}", stringDemoResult.getData());
        return stringDemoResult;
      //  DemoResult demoResult = fastDfsClientUtil.deleteFile("http://192.168.204.48:8888/group1/M00/00/93/wKjKS14j8OOAAkaZAABZ4IM3p0E95.xlsx");
      //  log.info("删除结果：{}",demoResult.toString());
    }


    /** 获取额外的信息
     * @Description
     * @Author zhaoshouyun
     * @Date 22:30 2020/4/20
     * @Param [auth]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String,Object> getExtraUserInfo(OAuth2Authentication auth){

        OAuth2AuthenticationDetails details
                = (OAuth2AuthenticationDetails) auth.getDetails();
        OAuth2AccessToken accessToken = tokenStore
                .readAccessToken(details.getTokenValue());
        return accessToken.getAdditionalInformation();
    }

    @RequestMapping("test")
    @ApiIgnore
    public DemoResult list(String id){
        DemoResult<Object> aosRmsUserDemoResult = uacFeignApi.listUser(id);
        return aosRmsUserDemoResult;
    }
    @GetMapping("test1")
    @ApiIgnore
    public DemoResult test(){
        return DemoResult.SUCCESS(zhao);
    }
}
